package com.yodawnla.roboAttack.scene;

import java.util.ArrayList;

import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.modifier.RotationModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.util.modifier.IModifier;

import android.view.KeyEvent;

import com.yodawnla.lib.YoScene;
import com.yodawnla.lib.util.tool.YoTimer;
import com.yodawnla.lib.util.widget.YoText;
import com.yodawnla.roboAttack.GameHud;
import com.yodawnla.roboAttack.Robot;
import com.yodawnla.roboAttack.system.SE;
import com.yodawnla.roboAttack.system.Values;
import com.yodawnla.roboAttack.system.YS;

public abstract class MyScene extends YoScene
{
	protected int mMod = Values.Mod.AirForward;
	protected AnimatedSprite mPilot;
	protected Robot mRobo;	
	protected AnimatedSprite mExplode;
	protected Sprite mLaser;
	protected PhysicsHandler mRoboPhyHandler;	
	protected YoText mTimerText;
	protected Sprite mSeat;
	protected ArrayList<Sprite> mAtkEffectList;
	
	public MyScene()
	{
		mLaserSoundTimer.start();
		mLaserSoundTimer.pause();
	}
	
	public void setMod(int mod)
	{
		mMod = mod;
	}	
	
	public void pilotIn()
	{		
	}
	
	public void pilotOut()
	{		
	}
	
	public void pilotUp()
	{
		if(mPilot == null)
			return;
		
		mPilot.stopAnimation(8);
	}
	
	public void openDoor()
	{ 
		Values.OPEN_DOOR=true;
		if(Values.PILOT_IN==true)
		{
			playSound(SE.Scream);
			mPilot.setVisible(true);
			mPilot.setPosition(!mRobo.getTextureRegion().isFlippedHorizontal()?mRobo.getX():mRobo.getX()+mRobo.getWidth()  ,mRobo.getY());
			mPilot.stopAnimation(9);

			mPilot.registerEntityModifier(new RotationModifier(1f, 0, 360));
			mPilot.registerEntityModifier(new MoveModifier(2f, mPilot.getX(), !mRobo.getTextureRegion().isFlippedHorizontal()?-200:600, mPilot.getY(), 800));
			Values.PILOT_IN = false;
			Values.PILOT_ALIVE=false; 
		}
	}
	
	public void closeDoor() 
	{ 
		Values.OPEN_DOOR=false;
	}
	
	public void emergencyExit()
	{ 
		if(Values.PILOT_IN==true)
		{ 
			
			if(Values.SEAT_JECTION==true)
			{ 
				playSound(SE.Scream);
				mPilot.setVisible(true);
				Values.PILOT_ALIVE = false;
			}
			else
			{
				playSound(SE.RocketLaunch);
				mSeat.setVisible(true);
				mSeat.registerEntityModifier(new MoveModifier(2f, mRobo.getX()+mRobo.getWidth()/2, mRobo.getX()+mRobo.getWidth()/2, mRobo.getY(), -800));
				Values.SEAT_JECTION=true;
				mPilot.setVisible(false);
			}
			
			mPilot.setPosition(mRobo.getX()+mRobo.getWidth()/2  ,mRobo.getY() );
			mPilot.stopAnimation();
			mPilot.setCurrentTileIndex(9); 
			
			mPilot.registerEntityModifier(new MoveModifier(2f, mPilot.getX(), mPilot.getX(), mPilot.getY(), -800));
			Values.PILOT_IN = false;
			Values.SEAT_JECTION=true;
		}
		else if(Values.PILOT_IN==false && Values.SEAT_JECTION==false) 
		{
			playSound(SE.RocketLaunch);
			mSeat.setVisible(true);
			mSeat.registerEntityModifier(new MoveModifier(2f, mRobo.getX()+mRobo.getWidth()/2, mRobo.getX()+mRobo.getWidth()/2, mRobo.getY(), -800));
			Values.SEAT_JECTION=true;
		}
	}
	
	public void forwardDown()
	{ 
		mRobo.setFlippedHorizontal(false);
		mRobo.animate(new long[]{250,250,250,250,250,250}, 3, 8, true); 
		mRoboPhyHandler.setVelocityX(50);
		checkLaserRotation();
	}
	
	public void forwardUp()
	{ 
		stopSound(SE.Walk);
		mRoboPhyHandler.setVelocityX(0);
		mRobo.stopAnimation(0);
	}
	
	public void backwardDown()
	{ 
 
		mRobo.setFlippedHorizontal(true);
		mRobo.animate(new long[]{250,250,250,250,250, 250}, 3, 8, true);
		mRoboPhyHandler.setVelocityX(-50);
		checkLaserRotation();
	}
	
	public void backwardUp()
	{ 
		stopSound(SE.Walk);
		mRoboPhyHandler.setVelocityX(0);
		mRobo.stopAnimation(0);
	}
	
	public abstract void takeOff();
	public abstract void landing();
	public void attackEnemy()
	{ 
		mRobo.animate(new long[]{25,100, 200, 25}, new int[]{0,1, 2, 0}, 0);
		mRoboPhyHandler.setVelocity(0);
	}
	
	public void superLaser()
	{ 
		if(Values.ROBO_ALIVE ==true)
		{
			if (mCanPlayLaserSound)
			{
				playSound(SE.Laser);
				mLaserSoundTimer.restart();
				mCanPlayLaserSound = false;
			}
			mRobo.stopAnimation(8);
			mLaser.setVisible(true);
			mLaser.clearEntityModifiers();
			mLaser.registerEntityModifier(new ScaleModifier(3, 4, 4, 0.1f,1f,new IEntityModifierListener()
			{
				public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem)
				{
				}

				public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem)
				{
					mLaser.setVisible(false);
				}
			}));
		}
	}
	
	boolean mCanPlayLaserSound = true;
	YoTimer mLaserSoundTimer = new YoTimer(3)
	{
		public void onTimePassed()
		{
			mCanPlayLaserSound = true;
			pause();
		}
	};
	
	public void selfDestruct()
	{ 
        playSound(SE.Explosion3);
		GameHud.getInstance().setEnabled(false);
		shakeCamera(2, 20);
		
		if(Values.ROBO_ALIVE == true)
		{
			GameHud.getInstance().showFullExpolsion();
			GameHud.getInstance().setHp(0);
			if(Values.PILOT_IN==true)
			{				 
				mPilot.setVisible(true);
				mPilot.setCurrentTileIndex(9);
				mPilot.registerEntityModifier(new RotationModifier(0.8f, 0, 360));
				mPilot.registerEntityModifier(new MoveModifier(0.8f, mPilot.getX() , -100,mPilot.getY(), -100));
				Values.PILOT_ALIVE=false;
				Values.PILOT_IN=false;
			}
			
			Values.ROBO_ALIVE = false;
			mRoboPhyHandler.setVelocity(0);
			mExplode.setVisible(true);
			mExplode.animate(60,false);
			mRobo.clearEntityModifiers();
			mRobo.registerEntityModifier(new AlphaModifier(5f, 1, 0,new IEntityModifierListener()
			{
				public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem)
				{
				}

				public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem)
				{
					mExplode.stopAnimation();
					mExplode.setVisible(false);
					mRobo.setVisible(false);
					toScene(YS.EndScene);
				}
			}));
		}
	}
	
	private void checkLaserRotation()
	{
		if(mRobo.getTextureRegion().isFlippedHorizontal())
			mLaser.setRotation(180);
		else
			mLaser.setRotation(0);
	}
	
	public void resetGame()
	{
		
		mPilot.clearEntityModifiers();
		mRobo.clearEntityModifiers();
		mRobo.setVisible(true);
		mRobo.stopAnimation(0);
		mSeat.setVisible(false);
		mLaser.clearEntityModifiers();
		mLaser.setVisible(false);
		mRoboPhyHandler.setVelocity(0, 0);
		mExplode.clearEntityModifiers();
		mExplode.stopAnimation(0);
		mExplode.setVisible(false);
		
	}
	
	 
	@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent)
	{
		switch (pKeyCode)
		{
		case KeyEvent.KEYCODE_BACK:
			if (pEvent.getAction() == KeyEvent.ACTION_DOWN)
			{
				GameHud.getInstance().resetGame();
				GameHud.getInstance().hide();
				toScene(YS.TitleScene);
				return true;
			}
			break;
		case KeyEvent.KEYCODE_MENU:
			if (pEvent.getAction() == KeyEvent.ACTION_DOWN)
			{
				return true;
			}
			break;
		}
		return false;
	}
}